
import threading
import SocketServer
import random


MIME_TYPE = 'application/x-hme'
MDNS_TYPE = '_tivo-hme._tcp.local.'
MDNS_DNSSD_TYPE = '_tivo-hme._tcp'
TIVO_DURATION = 'X-TiVo-Accurate-Duration'

class mylogger:
    def __init__(self):
        self.log_fileraw = 'myloggerraw.txt'
        self.log_filehex = 'myloggerhex.txt'
    
    def logthis(self, msg, bound):
        import time
        self.fo = file(self.log_fileraw, 'a')
        self.fo.write(bound + ' ' + time.strftime('%a, %b %d %Y, %I:%M:%S%p') + '****************\n' + str(msg) + '\nDone ' + bound + '****************\n')
        self.fo.close()
        
        self.fo = file(self.log_filehex, 'a')
        newmsg = ''
        for n in msg:
            newmsg = newmsg + str(hex(ord(n)))
        
        self.fo.write(bound + ' ' + time.strftime('%a, %b %d %Y, %I:%M:%S%p') + '****************\n' + str(newmsg) + '\nDone ' + bound + '****************\n')
        self.fo.close()
        

class mdnsHandler(SocketServer.BaseRequestHandler):
       
    def handle(self):
        lgr = mylogger()
        
        instr = self.request.recv(1024)
        lgr.logthis(instr, 'Incoming')
        print instr
        
        rndm = random.randrange(1000000000000000)
        rndm = str(rndm)
        nn = 'HTTP/1.1 200 Ok\r\n'
        nn = nn + 'Content-type: ' + MIME_TYPE + '\r\n'
        nn = nn + 'Set-Cookie: id=%s; path=/; expires=Tue, 26-Apr-2022 19:00:00 GMT\r\n\r\nSBTV\x00\x00\x00\x29'% rndm
        print 'sending...............'
        print nn
        lgr.logthis(nn, 'Outgoing')
        self.request.send(nn)
        print 'sent..................'
        
        print 'recving1..............'
        instr = self.request.recv(1024)
        lgr.logthis(instr, 'Incoming')
        print instr
        print 'done recv1............'
        
        print 'recving2..............'
        instr = self.request.recv(1024)
        lgr.logthis(instr, 'Incoming')
        print instr
        print 'done recv2............'

        print 'closing connection....'
        lgr.logthis('Closing Connection', 'Internal')
        self.request.close()        

        
class ParentThreadder:
    def __init__(self, threadname, port):
        self.threadname = threadname
        self.port = port
    
    def StartAThreadTCP(self):
        svr = SocketServer.TCPServer(('localhost', self.port), mdnsHandler)
        svr.serve_forever()
        
    def StartAThreadUDP(self):
        svr = SocketServer.UDPServer(('localhost', self.port), mdnsHandler)
        svr.serve_forever()
        
TCP_LISTEN_PORTS = [80, 8080, 8081, 8082, 5354, 37, 443, 2190, 7287, 7288, 5223, 5353, 4430, 8200]
UDP_LISTEN_PORTS = [37, 2190, 5353, 5354, 7288]

print 'Thread Count Before Start: %d'% threading.activeCount()

for port in TCP_LISTEN_PORTS:
    tempObj = ParentThreadder('Steve\'s Thread For TCP Port: %d'% port, port)
    threadObj = threading.Thread(name=tempObj.threadname, target=tempObj.StartAThreadTCP)
    threadObj.start()

for port in UDP_LISTEN_PORTS:
    tempObj = ParentThreadder('Steve\'s Thread For UDP Port: %d'% port, port)
    threadObj = threading.Thread(name=tempObj.threadname, target=tempObj.StartAThreadUDP)
    threadObj.start()

    
print ' Thread Count After Start: %d'% threading.activeCount()

threaded_sort = threading.enumerate()
threaded_sort.sort()

for n in threaded_sort:
    print n.getName()
    
    
    
    
    
    
    
    
    
    
    
    
#scksvr = SocketServer.TCPServer(('localhost', 7288), mdnsHandler)
#scksvr.serve_forever()